const { notEmpty } = require('../utils.js')

module.exports = {
  description: 'generate a api',
  prompts: [
    {
      type: 'input',
      name: 'name',
      message: 'api name please',
    },
    {
      type: 'checkbox',
      name: 'blocks',
      message: 'Blocks:',
      choices: [
        {
          name: 'request',
          value: 'request',
          checked: true,
        },
        {
          name: 'list',
          value: 'list',
          checked: true,
        },
        {
          name: 'add',
          value: 'add',
          checked: true,
        },
        {
          name: 'edit',
          value: 'edit',
          checked: true,
        },
        {
          name: 'delete',
          value: 'delete',
          checked: true,
        },
      ],
      validate(value) {
        if (!value.includes('request')) {
          return 'request require'
        }
        return true
      },
    },
  ],
  actions: (data) => {
    const name = '{{name}}'
    const { blocks } = data
    const actions = [
      {
        type: 'add',
        path: `src/api/${name}.js`,
        templateFile: 'plop-templates/api/index.hbs',
        data: {
          request: blocks.includes('request'),
          list: blocks.includes('list'),
          add: blocks.includes('add'),
          edit: blocks.includes('edit'),
          delete: blocks.includes('delete'),
        },
      },
    ]

    return actions
  },
}
